<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="openImportTable(true)"> 导入</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                {{ t('action.delete') }}
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>{{ t('action.batchOpt') }}
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <CodeGenTableModal @register="registerModal" @success="handleSuccess"></CodeGenTableModal>
    <!--预览-->
    <PreviewModal @register="registerPreviewModal" />
    <!--导入-->
    <ImportTableModal @register="registerImportTableModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { useModal } from '/@/components/Modal';
import { useMessage } from '/@/hooks/web/useMessage'
import { useI18n } from '/@/hooks/web/useI18n'
import CodeGenTableModal from './modules/CodeGenTableModal.vue'
import PreviewModal from './modules/PreviewModal.vue'
import ImportTableModal from './modules/ImportTableModal.vue'
import { columns, searchFormSchema } from './CodeGenTable.data';
import { list, downloadCodegen, deleteOne, batchDelete, copyAndSave, getImportUrl, getExportUrl,syncFromDb, } from './CodeGenTable.api';
//注册model
const [registerModal, { openModal }] = useModal();
const [registerPreviewModal, { openModal: openPreviewModal }] = useModal()
const [registerImportTableModal, { openModal: openImportTable }] = useModal()
const { createMessage } = useMessage()
const { t } = useI18n()
//注册table数据
const { tableContext } = useListPage({
  tableProps: {
    title: '模块信息',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    actionColumn: {
      width: 200,
      fixed: 'right'
    },
  },
  exportConfig: {
    name: "模块信息",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext


/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}

async function handleCopyAndSave(record: Recordable) {
  await copyAndSave({ id: record.id }, reload);
}

/**
 * 生成代码
 * @param record 
 */
async function handleGenTable(record: Recordable) {
  await downloadCodegen(record)
  createMessage.success(t('common.successText'))
}

function handlePreview(record: Recordable) {
  openPreviewModal(true, { record })
}

async function handleSync(record: Recordable) {
 await syncFromDb(record, reload);
}

/**
 * 详情
*/
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, reload);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, reload);
}
/**
 * 成功回调
 */
function handleSuccess() {
  reload();
}
/**
   * 操作栏
   */
function getTableAction(record) {
  console.log("record:",record)
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '预览',
      onClick: handlePreview.bind(null, record),
      ifShow:() => record.tableType != '5',
    }, {
      label: '生成',
      onClick: handleGenTable.bind(null, record),
      ifShow:() => record.tableType != '5',
    }
  ]
}
/**
   * 下拉操作栏
   */
function getDropDownAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    }
    , {
      label: '复制新增',
      onClick: handleCopyAndSave.bind(null, record),
    }, , {
      label: '同步',
      popConfirm: {
        title: '是否确认同步数据库字段?',
        confirm: handleSync.bind(null, record),
      }
    }, {
      label: '删除',
      popConfirm: {
        title: '是否确认删除?',
        confirm: handleDelete.bind(null, record),
      }
    }
  ]
}
</script>

<style scoped></style>
